package com.mofang.activity.login;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.mofang.R;
import com.mofang.activity.BaseTitleViewActiviey;
import com.mofang.activity.MofangCommonUpdateView;
import com.mofang.activity.MyTabHostActivity;
import com.mofang.controller.UserController;
import com.pactera.framework.exception.IException;
import com.pactera.framework.util.ToastHelper;

public class FindPasswordActivity extends BaseTitleViewActiviey implements
		OnClickListener {
	private EditText enterPhone_edit; // 输入手机号
	private EditText enterCaptcha_edit;// 输入验证码
	private EditText enterPsw_edit;// 输入密码
	private EditText checkPsw_edit;// 确认密码
	private LinearLayout enterCaptcha_btn;// 发送验证码
	private Button btn_login_ok;// 确定
	private UserController userController;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_login_find_password);
		super.onCreate(savedInstanceState);
		setNavigationTitle("找回密码");
		setNavigationLeftBtnImage(R.drawable.return_arrow);
		userController = new UserController();
	}

	@Override
	protected void initView() {
		super.initView();
		enterPhone_edit = (EditText) findViewById(R.id.find_password_enterPhone_edit);
		enterCaptcha_edit = (EditText) findViewById(R.id.find_password_enterCaptcha_edit);
		enterPsw_edit = (EditText) findViewById(R.id.find_password_enterPsw_edit);
		checkPsw_edit = (EditText) findViewById(R.id.find_password_checkPsw_edit);
		enterCaptcha_btn = (LinearLayout) findViewById(R.id.find_password_enterCaptcha_btn);
		btn_login_ok = (Button) findViewById(R.id.btn_login_ok);

	}

	@Override
	protected void setListener() {
		super.setListener();
		enterCaptcha_btn.setOnClickListener(this);
		btn_login_ok.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.find_password, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		String phoneNo = "";
		Intent intent;
		switch (v.getId()) {
		case R.id.find_password_enterCaptcha_btn:
			// 发送验证码
			userController.sendVerificationCode(new VerificationCodeUpdateView(
					mContext), phoneNo);
			break;
		case R.id.btn_login_ok:
			String verificationCode = "";
			String newPassword = "";
			userController.findPassword(new FindPasswordUpdateView(mContext),
					phoneNo, newPassword, verificationCode);

			//
			// intent = new Intent(getApplicationContext(),
			// LoginActivity.class);
			// startActivity(intent);

			break;

		}

	}

	private class VerificationCodeUpdateView extends
			MofangCommonUpdateView<Boolean> {
		private ProgressDialog progressDialog;

		public VerificationCodeUpdateView(Context context) {
			super(context);
		}

		@Override
		public void onPreExecute() {
			super.onPreExecute();
			progressDialog = ProgressDialog.show(FindPasswordActivity.this,
					null, "正在发送验证码", true, false);
		}

		@Override
		public void onPostExecute(Boolean result) {
			// result=false;
			progressDialog.dismiss();
			if (result) {
				ToastHelper.getInstance().showLongMsg("发送成功");
			} else {
				viewHelper.showBTNDialog("发送失败，请重试");
			}
		}

		@Override
		public void handleException(IException ex) {
			viewHelper.showBTNDialog(ex.getMessage());
		}
	}

	private class FindPasswordUpdateView extends
			MofangCommonUpdateView<Boolean> {
		private ProgressDialog progressDialog;

		public FindPasswordUpdateView(Context context) {
			super(context);
		}

		@Override
		public void onPreExecute() {
			super.onPreExecute();
			progressDialog = ProgressDialog.show(FindPasswordActivity.this,
					null, "正在验证", true, false);
		}

		@Override
		public void onPostExecute(Boolean result) {
			// result=false;
			progressDialog.dismiss();
			if (result) {
				ToastHelper.getInstance().showLongMsg("修改成功");
			} else {
				viewHelper.showBTNDialog("修改失败，请重试");
			}
		}

		@Override
		public void handleException(IException ex) {
			viewHelper.showBTNDialog(ex.getMessage());
		}
	}
}
